package org.csu.mypetstore.persistence.impl;

import org.csu.mypetstore.domain.Orders;
import org.csu.mypetstore.persistence.OrderDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    private static String GET_ORDER_BY_USERNAME="select * from orders where userid=?";
    private static String GET_ORDER_BY_ITEMID="select * from orders where itemid=?";
    private static String INSERT_ORDER="insert into orders (userid,shipaddr1,shipaddr2,shipcity,shipstate,shipzip,shipcountry,billaddr1,billaddr2,billcity,billstate,billzip,billcountry,courier,totalprice,billtofirstname,billtolastname,shiptofirstname,shiptolastname,creditcard,exprdate,cardtype,locale,itemid) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    //private static String UPDATE_ORDER_LIST="update orders set =? where userid=? and itemid=?";
    //private static String SELECT_ITEM="select * from cart where userid=? and itemid=?";
    @Override
    public List<Orders> getOrderByUsername(String userid) {
        List<Orders> ordersList=new ArrayList<Orders>();
        Connection conn=null;
        PreparedStatement stm=null;
        ResultSet rs=null;

        try {
            conn=DBUtil.getConnection();
            stm=conn.prepareStatement(GET_ORDER_BY_USERNAME);
            stm.setString(1,userid);

            rs= stm.executeQuery();
            while(rs.next())
            {
                Orders orders=new Orders();
                orders.setOrderid(rs.getString("orderid"));
                orders.setUserid(rs.getString("userid"));
                orders.setShipaddr1(rs.getString("shipaddr1"));
                orders.setShipaddr2(rs.getString("shipaddr2"));
                orders.setShipcity(rs.getString("shipcity"));
                orders.setShipstate(rs.getString("shipstate"));
                orders.setShipzip(rs.getString("shipzip"));
                orders.setShipcountry(rs.getString("shipcountry"));
                orders.setBilladdr1(rs.getString("billaddr1"));
                orders.setBilladdr2(rs.getString("billaddr2"));
                orders.setBillcity(rs.getString("billcity"));
                orders.setBillstate(rs.getString("billstate"));
                orders.setBillzip(rs.getString("billzip"));
                orders.setBillcountry(rs.getString("billcountry"));
                orders.setCourier(rs.getString("courier"));
                orders.setTotalprice(Double.parseDouble((rs.getString("totalprice"))));
                orders.setBilltofirstname(rs.getString("billtofirstname"));
                orders.setBilltolastname(rs.getString("billtolastname"));
                orders.setShiptofirstname(rs.getString("shiptofirstname"));
                orders.setShiptolastname(rs.getString("shiptolastname"));
                orders.setCreditcard(rs.getString("creditcard"));
                orders.setExprdate(rs.getString("exprdate"));
                orders.setCardtype(rs.getString("cardtype"));
                orders.setLocale(rs.getString("locale"));
                orders.setTotalprice(rs.getDouble("itemid"));

                ordersList.add(orders);
            }
            DBUtil.close(conn,stm,rs);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ordersList;
    }

    @Override
    public Orders getOrder(String itemid) {
        Orders orders=new Orders();
        Connection conn=null;
        PreparedStatement stm=null;
        ResultSet rs=null;

        try {
            conn=DBUtil.getConnection();
            stm=conn.prepareStatement(GET_ORDER_BY_ITEMID);
            stm.setString(1,itemid);

            rs=stm.executeQuery();
            if(rs.next())
            {
                orders.setOrderid(rs.getString("orderid"));
                orders.setUserid(rs.getString("userid"));
                orders.setShipaddr1(rs.getString("shipaddr1"));
                orders.setShipaddr2(rs.getString("shipaddr2"));
                orders.setShipcity(rs.getString("shipcity"));
                orders.setShipstate(rs.getString("shipstate"));
                orders.setShipzip(rs.getString("shipzip"));
                orders.setShipcountry(rs.getString("shipcountry"));
                orders.setBilladdr1(rs.getString("billaddr1"));
                orders.setBilladdr2(rs.getString("billaddr2"));
                orders.setBillcity(rs.getString("billcity"));
                orders.setBillstate(rs.getString("billstate"));
                orders.setBillzip(rs.getString("billzip"));
                orders.setBillcountry(rs.getString("billcountry"));
                orders.setCourier(rs.getString("courier"));
                orders.setTotalprice(Integer.valueOf(rs.getString("totalprice")));
                orders.setBilltofirstname(rs.getString("billtofirstname"));
                orders.setBilltolastname(rs.getString("billtolastname"));
                orders.setShiptofirstname(rs.getString("shiptofirstname"));
                orders.setShiptolastname(rs.getString("shiptolastname"));
                orders.setCreditcard(rs.getString("creditcard"));
                orders.setExprdate(rs.getString("exprdate"));
                orders.setCardtype(rs.getString("cardtype"));
                orders.setLocale(rs.getString("lacale"));
            }
            DBUtil.close(conn,stm,rs);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return orders;
    }

    @Override
    public void insertOrder(Orders orders) {
        Connection conn=null;
        PreparedStatement stm=null;
        ResultSet rs=null;
        try {
            conn=DBUtil.getConnection();
            stm=conn.prepareStatement(INSERT_ORDER);
//            stm.setString(1,orders.getOrderid());
            stm.setString(1,orders.getUserid());
            stm.setString(2,orders.getShipaddr1());
            stm.setString(3,orders.getShipaddr2());
            stm.setString(4,orders.getShipcity());
            stm.setString(5,orders.getShipstate());
            stm.setString(6,orders.getShipzip());
            stm.setString(7,orders.getShipcountry());
            stm.setString(8,orders.getBilladdr1());
            stm.setString(9,orders.getBilladdr2());
            stm.setString(10,orders.getBillcity());
            stm.setString(11,orders.getBillstate());
            stm.setString(12,orders.getBillzip());
            stm.setString(13,orders.getBillcountry());
            stm.setString(14,orders.getCourier());
            stm.setDouble(15,orders.getTotalprice());
            stm.setString(16,orders.getBilltofirstname());
            stm.setString(17,orders.getBilltolastname());
            stm.setString(18,orders.getShiptofirstname());
            stm.setString(19,orders.getShiptolastname());
            stm.setString(20,orders.getCreditcard());
            stm.setString(21,orders.getExprdate());
            stm.setString(22,orders.getCardtype());
            stm.setString(23,orders.getLocale());
            stm.setString(24,orders.getItemid());

            stm.executeUpdate();

            DBUtil.close(conn,stm,rs);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void insertOrderStatus(Orders orders) {

    }

    @Override
    public boolean selectOrderList(Orders orders) {
        return false;
    }

    @Override
    public void updateOrders(Orders orders) {

    }
}
